package org.jeecg.modules.zcgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgBaseController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.zcgl.entity.RepairImage;
import org.jeecg.modules.zcgl.entity.RepairInfo;
import org.jeecg.modules.zcgl.enums.RepairHandleStatusEnum;
import org.jeecg.modules.zcgl.service.IRepairImageService;
import org.jeecg.modules.zcgl.service.IRepairInfoService;
import org.jeecg.modules.zcgl.vo.RepairInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 维修信息
 * @Author: jeecg-boot
 * @Date:   2020-06-19
 * @Version: V1.0
 */
@Api(tags="维修信息")
@RestController
@RequestMapping("/zcgl/repairInfo")
@Slf4j
public class RepairInfoController extends JeecgBaseController<RepairInfo, RepairInfo, IRepairInfoService> {

	@Autowired
	private IRepairInfoService repairInfoService;
	@Autowired
	private IRepairImageService repairImageService;
	
	/**
	 * 分页列表查询
	 *
	 * @param repairInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "维修信息-分页列表查询")
	@ApiOperation(value="维修信息-分页列表查询", notes="维修信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(RepairInfo repairInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RepairInfo> queryWrapper = QueryGenerator.initQueryWrapper(repairInfo, req.getParameterMap());
		Page<RepairInfo> page = new Page<RepairInfo>(pageNo, pageSize);
		IPage<RepairInfo> pageList = repairInfoService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param vo
	 * @return
	 */
	@AutoLog(value = "维修信息-添加")
	@ApiOperation(value="维修信息-添加", notes="维修信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody RepairInfoVo vo) {
		repairInfoService.saveByVo(vo);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param vo
	 * @return
	 */
	@AutoLog(value = "维修信息-编辑")
	@ApiOperation(value="维修信息-编辑", notes="维修信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody RepairInfoVo vo) {
		repairInfoService.updateByVo(vo);
		return Result.ok("编辑成功!");
	}

	/**
	 * 安排维修人员
	 * @param repairInfo
	 * @return
	 */
	@AutoLog(value = "维修信息-安排维修人员")
	@ApiOperation(value="维修信息-安排维修人员", notes="维修信息-安排维修人员")
	@PostMapping(value = "/handleUserId")
	public Result<?> handleUserId(@RequestBody RepairInfo repairInfo) {
		if (StringUtils.isBlank(repairInfo.getHandleUserId())){
			return Result.error("请选择维修人员");
		}
		RepairInfo entity = repairInfoService.getById(repairInfo.getId());
		if (entity == null){
			return Result.error("未查询到信息");
		}
		if (entity.getHandleStatus() != null && RepairHandleStatusEnum.PENDING.getStatus() != entity.getHandleStatus().intValue()
		&& RepairHandleStatusEnum.SYS_USER.getStatus() != entity.getHandleStatus().intValue()){
			return Result.error("维修信息已处理，安排人员失败！");
		}
		entity.setHandleUserId(repairInfo.getHandleUserId());
		entity.setHandleStatus(RepairHandleStatusEnum.HANDLE_ING.getStatus());
		repairInfoService.updateById(entity);
		return Result.ok("安排成功!");
	}

	/**
	 * 维修反馈
	 * @param vo
	 * @return
	 */
	@AutoLog(value = "维修信息-维修反馈")
	@ApiOperation(value="维修信息-维修反馈", notes="维修信息-维修反馈")
	@PostMapping(value = "/feedback")
	public Result<?> feedback(@RequestBody RepairInfoVo vo) {
	    if (StringUtils.isBlank(vo.getFeedbackContent())){
	        return Result.error("请填写维修反馈的信息说明！");
        }
		repairInfoService.feedback(vo);
		return Result.ok("反馈成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "维修信息-通过id删除")
	@ApiOperation(value="维修信息-通过id删除", notes="维修信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		repairInfoService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "维修信息-批量删除")
	@ApiOperation(value="维修信息-批量删除", notes="维修信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.repairInfoService.removeByIds(Arrays.asList(ids.split(CommonConstant.DEFAULT_SEPARATOR)));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "维修信息-通过id查询")
	@ApiOperation(value="维修信息-通过id查询", notes="维修信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RepairInfo> queryById(@RequestParam(name="id",required=true) String id) {
		RepairInfo repairInfo = repairInfoService.getById(id);
		if(repairInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(repairInfo);
	}

	@AutoLog(value = "反馈图片通过主表ID查询")
	@ApiOperation(value = "反馈图片主表ID查询", notes = "反馈图片-通主表ID查询")
	@GetMapping(value = "/queryRepairImageByMainId")
	public Result<List<RepairImage>> queryRepairImageByMainId(@RequestParam(name = "id", required = true) String id,
																   @RequestParam(name = "type", required = true) Integer type,
																   @RequestParam(name = "repairFeedbackId", required = false) String repairFeedbackId) {
		List<RepairImage> assetInfoSubList = repairImageService.selectByMainId(id, type, repairFeedbackId);
		return Result.ok(assetInfoSubList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param repairInfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RepairInfo repairInfo) {
        return super.exportXls(request, repairInfo, RepairInfo.class, "维修信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RepairInfo.class);
    }

	@Override
	public List<RepairInfo> exportList(List<RepairInfo> list) {
		return list;
	}

	@Override
	public boolean importSave(RepairInfo repairInfo) {
		return repairInfoService.save(repairInfo);
	}
}
